type Toptions_Ppanel$title = Toptions_Ptext | Toptions_Ptext$text;
type Toptions_Ppanel$icon = Toptions_Picon | Toptions_Picon$icon;
type Toptions_Ppanel$content = string | HTMLElement | KHTMLElement;

type Toptions_Ppanel = {
    title?: Toptions_Ppanel$title;
    icon?: Toptions_Ppanel$icon;
    content?: Toptions_Ppanel$content | Toptions_Ptext;
    closeable?: boolean;
    destroy?: boolean;
};

class Ppanel extends _CP_<Toptions_Ppanel> {
    // ====================================================== 当前控件特殊实现 ====================================================== //

    // ====================================================== 初始化参数 ====================================================== //
    static defaultOptions: Toptions_Ppanel = {
    };
    protected override options_default(): Toptions_Ppanel {
        return Ppanel.defaultOptions;
    }
    protected override options_handlers(): Toptions_handlers<Toptions_Ppanel> {
        return {
            title: {
                [Etype.string]: U.F.fn.obj,
                [Etype.obj]: U.F.fn.obj
            },
            icon: {
                [Etype.string]: U.F.fn.obj,
                [Etype.obj]: U.F.fn.obj
            },
            content: {
                [Etype.string]: U.F.fn.obj,
                [Etype.HTMLElement]: U.F.fn.obj,
                [Etype.KHTMLElement]: U.F.fn.obj,
                [Etype.obj]: U.F.fn.obj,
            },
            closeable: {
                [Etype.boolean]: U.F.fn.obj,
            },
            destroy: {
                [Etype.boolean]: U.F.fn.obj,
            },
        };
    }
    protected override overloading_mapping(mapping: Toption_key_to_types_mapping<Toptions_Ppanel>): Toverloading_mapping<Toptions_Ppanel> {
        return [
            [
                { key: 'content', types: [Etype.string, Etype.HTMLElement, Etype.KHTMLElement] },
            ],
        ];
    }
    protected override options_checker(options: Toptions_Ppanel): void {
    }

    // ====================================================== 创建实例对象 ====================================================== //
    protected override initializer(): void {
        const ele_title_bar = U.D.createElement('div').addClass('title-bar').insertTo(this.ele);
        const ele_title_wrapper = U.D.createElement('div').addClass('title-wrapper').insertTo(ele_title_bar);
        const ele_title_controls = U.D.createElement('div').addClass('title-controls').insertTo(ele_title_bar);
        const ele_content_bar = U.D.createElement('div').addClass('content-bar').insertTo(this.ele);

        // 图标
        if (Object.prototype.hasOwnProperty.call(this.options, 'icon')) {
            K.icon(this.options.icon as Toptions_Picon).insertTo(ele_title_wrapper);
        }

        // 标题
        if (Object.prototype.hasOwnProperty.call(this.options, 'title')) {
            K.text(this.options.title).insertTo(ele_title_wrapper);
        }

        // 关闭按钮
        if (this.options.closeable) {
            K.button({
                icon: 'clear',
                onclick: () => {
                    if (this.options.destroy) {
                        this.ele.ele()!.remove();
                    } else {
                        this.ele.hide();
                    }
                }
            }).insertTo(ele_title_controls);
        }

        // 内容
        if (Object.prototype.hasOwnProperty.call(this.options, 'content')) {
            const t = U.typeof(this.options.content);
            switch (t) {
                case Etype.string:
                    ele_content_bar.setAttr('textContent', this.options.content as string);
                    break;
                case Etype.HTMLElement:
                    ele_content_bar.insertElement(this.options.content as HTMLElement);
                    break;
                case Etype.KHTMLElement:
                    (this.options.content as KHTMLElement).forEach(ele => {
                        ele_content_bar.insertElement(ele);
                    });
                    break;
                default:
                    K.text(this.options.content).insertTo(ele_content_bar);
                    break;
            }
        }
    }
    protected override afterInitializer(): void {
    }
}

interface _IK_ {
    // ========= 创建实例对象（通用） ========= //
    /**
     * 创建控件
     * @returns 控件实例对象
     */
    panel(): KHTMLElement;

    /**
     * 创建控件
     * @param options 控件参数
     * @returns 控件实例对象
     */
    panel(options: Toptions_Ppanel): KHTMLElement;

    // ========= 创建实例对象（特殊） ========= //
    /**
     * 创建控件
     * @param content 内容
     * @returns 控件实例对象
     */
    panel(content: Toptions_Ppanel$content): KHTMLElement;
}
{
    K.panel = (...args: Array<any>) => { return new Ppanel(...args).ele; };
}
